// 下拉列表

var oDiv = $("#u35");
var oUl = $("#ul1");
oDiv.click(function () {
  oUl.slideToggle();
});
$(".div2").click(function () {
  $(".ul2").slideToggle();
});
$(".div3").click(function () {
  $(".ul3").slideToggle();
});
$(".div4").click(function () {
  $(".ul4").slideToggle();
});
$(".div5").click(function () {
  $(".ul5").slideToggle();
});
$(".div6").click(function () {
  $(".ul6").slideToggle();
});

//表格的数据
var data = [
  {
    id: 1,
    encode: "GRBL20180319102942699",
    classify: "医疗医技",
    department: "新生儿小婴儿科",
    reportDate: "2022-03-23",
    endDate: "2022-04-16",
    state: "已通过",
  },
  {
    id: 2,
    encode: "GRBL20180319102942696",
    classify: "护理异常",
    department: "新生儿小婴儿科",
    reportDate: "2022-03-16",
    endDate: "2022-04-16",
    state: "待处理",
  },
  {
    id: 3,
    encode: "GRBL20180319102942696",
    classify: "输血不良",
    department: "血液科",
    reportDate: "2033-03-16",
    endDate: "2018-04-16",
    state: "待处理",
  },
  {
    id: 4,
    encode: "GRBL20180319102942699",
    classify: "ICU病房",
    department: "血液科",
    reportDate: "2021-03-16",
    endDate: "2021-04-16",
    state: "已通过",
  },
  {
    id: 5,
    encode: "GRBL20180319102942699",
    classify: "医疗医技",
    department: "新生儿小婴儿科",
    reportDate: "2022-03-16",
    endDate: "2022-04-16",
    state: "已通过",
  },
  {
    id: 6,
    encode: "GRBL20180319102942699",
    classify: "药物不良",
    department: "康复科",
    reportDate: "2018-04-16",
    endDate: "2018-04-16",
    state: "已通过",
  },
  {
    id: 7,
    encode: "GRBL20180319102942699",
    classify: "药物不良",
    department: "康复科",
    reportDate: "2018-04-16",
    endDate: "2018-04-16",
    state: "已通过",
  },
  {
    id: 8,
    encode: "GRBL20180319102942699",
    classify: "医疗医技",
    department: "新生儿小婴儿科",
    reportDate: "2018-04-16",
    endDate: "2018-04-16",
    state: "待处理",
  },
  {
    id: 9,
    encode: "GRBL20180319102942699",
    classify: "ICU病房",
    department: "血液科",
    reportDate: "2018-04-16",
    endDate: "2018-04-16",
    state: "待处理",
  },
  {
    id: 10,
    encode: "GRBL20180319102942699",
    classify: "医疗医技",
    department: "新生儿小婴儿科",
    reportDate: "2018-04-16",
    endDate: "2018-04-16",
    state: "待处理",
  },
  {
    id: 11,
    encode: "GRBL20180319102942699",
    classify: "医疗医技",
    department: "新生儿小婴儿科",
    reportDate: "2018-04-16",
    endDate: "2018-04-16",
    state: "已通过",
  },
  {
    id: 12,
    encode: "GRBL20180319102942699",
    classify: "药物不良",
    department: "肾脏免疫科",
    reportDate: "2018-04-10",
    endDate: "2018-04-10",
    state: "已通过",
  },
  {
    id: 13,
    encode: "GRBL20180319102942699",
    classify: "药物不良",
    department: "肾脏免疫科",
    reportDate: "2018-04-10",
    endDate: "2018-04-10",
    state: "待处理",
  },
  {
    id: 14,
    encode: "GRBL20180319102942699",
    classify: "医疗医技",
    department: "新生儿小婴儿科",
    reportDate: "2018-04-16",
    endDate: "2018-04-16",
    state: "已通过",
  },
  {
    id: 15,
    encode: "GRBL20180319102942699",
    classify: "医疗医技",
    department: "新生儿小婴儿科",
    reportDate: "2018-04-16",
    endDate: "2018-04-16",
    state: "待处理",
  },
  {
    id: 16,
    encode: "GRBL20180319102942699",
    classify: "医疗器械",
    department: "康复科",
    reportDate: "2018-04-16",
    endDate: "2018-04-16",
    state: "待处理",
  },
  {
    id: 17,
    encode: "GRBL20180319102942699",
    classify: "医疗器械",
    department: "新生儿小婴儿科",
    reportDate: "2018-04-16",
    endDate: "2018-04-16",
    state: "待处理",
  },
  {
    id: 18,
    encode: "GRBL20180319102942699",
    classify: "医疗医技",
    department: "血液科",
    reportDate: "2018-04-16",
    endDate: "2018-04-16",
    state: "已通过",
  },
];

var page = 1;

var count = 5;

var oTbody = document.querySelector("tbody");

var oPagination = document.querySelector(".pagination");

var oLeft = document.querySelector(".left");
var oRight = document.querySelector(".right");
//获取输入框
var inquireBtn = document.querySelector(".inquireBtn");
var oSearchInput = document.querySelectorAll(".searchInput");
//清除
var oClearBtn = document.querySelector(".clearBtn");
//再次发起
var launchBtn = document.querySelector(".launch");
// console.log(oClearBtn)
var aDel = document.querySelector(".del");
console.log(aDel);

function render() {
  var str = oSearchInput[0].value;
  var str1 = oSearchInput[1].value;
  var str2 = oSearchInput[2].value;
  var str3 =oSearchInput[3].value 
  var str4 =oSearchInput[4].value
     
  var str5 = oSearchInput[5].value;

  var searchData = [];
  var data1 = [];
  var data2 = [];
  var data3 = [];
  var data4 = [];
  var data5 = [];

  if (str.trim() == "") {
    data1 = data;
  } else {
    data1 = data.filter(function (v) {
      return v.encode.includes(str);
    });
  }
  // console.log(data1);
  if (str1 == "--请选择--") {
    data2 = data1;
  } else {
    data2 = data1.filter(function (v) {
      return v.classify.includes(str1);
    });
  }
  // console.log(data2);
  if (str2 == "--请选择--") {
    data3 = data2;
  } else {
    data3 = data2.filter(function (v) {
      return v.department.includes(str2);
    });
  }
  console.log(data3)
 if(str3.trim()==''){
    data4=data3
 } else{
  data4=data3.filter(function(v){
    // var time=new Date(v.reportDate).getTime();
    //  return  time<str3
    return v.reportDate.includes(str3)
  })
  
 }
 console.log(data4)
  if(str4.trim()==''){
     data5=data4
  }else{
    data5=data4.filter(function(v){
      
        return v.endDate.includes(str4)
    })
  }
  console.log(data5)

  if(str5=='--请选择--'){
    searchData=data5
  }else{
    searchData=data5.filter(function(v){
     return v.state.includes(str5)
    })
  }

  //渲染列表数据函数

  oTbody.innerHTML = "";
  //计算每一页出现多少数据
  var renderArr = searchData.slice((page - 1) * count, page * count);
  //   console.log(renderArr);
  for (var i = 0; i < renderArr.length; i++) {
    var oTr = document.createElement("tr");
    oTr.innerHTML = `
          <td>${renderArr[i].id}</td>
          <td>${renderArr[i].encode}</td>
          <td>${renderArr[i].classify}</td>
          <td>${renderArr[i].department}</td>
          <td>${renderArr[i].reportDate}</td>
          <td>${renderArr[i].endDate}</td>
          <td>${renderArr[i].state}</td>
          <td>
              <button type="button" class="btn btn-default"><a href="../pages/infectevent.html" style="text-decoration: none;
              color: #333333;">查看</a> </button>

              <button type="button" class="btn btn-default" data-toggle="modal" data-target="#loadModal">
              下载</button>
              <div class="modal fade" id="loadModal" tabindex="-1" role="dialog" >
              <div class="modal-dialog" role="document">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">下载页面</h4>
                  </div>
                  <div class="modal-body">
                    <table class="table table-bordered" style='width:95%'>
                      <thead style="background: #868686;">
                          <tr>
                              <th><input type="checkbox"></th>
                              <th>上报科室</th>
                              <th>追踪状态</th>
                          </tr>
                      </thead>
                      <tbody>
                          <tr>
                              <td><input type="checkbox"></td>
                              <td>医务科</td>
                              <td>已审批</td>
                          </tr>
                      </tbody>
                    </table>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary loadBtn">下载</button>
                  </div>
                </div>
              </div>
          </div>

                
               <button type="button" class="btn btn-default"data-toggle="modal" data-target="#launchModal" >再次发起</button>
  
               <button type="button" class="btn btn-default del2 "data-toggle="modal" data-target="#delModal" > 删除</button>

               
          </td>`;
    oTbody.appendChild(oTr);
  }

  //渲染页码
  var oPage = document.querySelectorAll(".pageBtn");
  for (var i = 0; i < oPage.length; i++) {
    oPagination.removeChild(oPage[i].parentNode);
  }

  for (var i = 1; i <= Math.ceil(searchData.length / count); i++) {
    // console.log(data.length )
    var oLi = document.createElement("li");
    oLi.innerHTML = `<a class="pageBtn" href="#">${i}</a>`;

    if (i == page) {
      oLi.classList.add("active");
    }
    oPagination.insertBefore(oLi, oPagination.lastElementChild);
  }
}
render();

//点击页码
oPagination.addEventListener("click", changePage);
function changePage() {
  //   console.log(event.target);
  if (event.target.className == "pageBtn") {
    page = event.target.innerText;
    render();
  }
}

//向左
oLeft.addEventListener("click", leftBtn);
oRight.addEventListener("click", rightBtn);
function leftBtn() {
  console.log(page);
  if (page > 1) {
    page--;
    render();
  }
}

function rightBtn() {
  if (page < Math.ceil(data.length / count)) {
    page++;
    render();
  }
}

//删除功能
oTbody.addEventListener("click", delFn);
function delFn() {
  if (event.target.className.includes("del2")) {
    var id = event.target.parentNode.parentNode.children[0].innerText;
  }
  aDel.addEventListener("click", deleteFn);
  function deleteFn() {
    for (var i = 0; i < data.length; i++) {
      if (data[i].id == id) {
        console.log(data[i].id, id);
        data.splice(i, 1);
        $("#delModal").modal("hide");
      }
    }
    render();
  }
}

//下载
oTbody.addEventListener("click", loadBtn);
function loadBtn() {
  if (event.target.className.includes("loadBtn")) {
    $("#loadModal").modal("hide");
  }
}
//再次发起
launchBtn.addEventListener("click", launchFn);
function launchFn() {
  $("#launchModal").modal("hide");
}

//查询
inquireBtn.addEventListener("click", inquireBtnFn);

function inquireBtnFn() {
  page = 1;
  render();
}

//清除
oClearBtn.addEventListener("click", clearFn);
function clearFn() {
  oSearchInput[0].value = "";
  oSearchInput[1].value = "--请选择--";
  oSearchInput[2].value = "--请选择--";
  oSearchInput[3].value = "";
  oSearchInput[4].value = "";
  oSearchInput[5].value = "--请选择--";
  render();
}

//查看
